<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Http\Model\Relation;
use App\Http\Model\User;
use App\Http\Services\MsgServices;

class CommandUserRelationCheck extends Command
{
    protected $signature = 'CommandUserRelationCheck';

    protected $description = '每分钟检查用户亲密关系';

    public function __construct()
    {
        parent::__construct();
    }

    public function handle()
    {
        $relationModel = new Relation();
        $userModel = new User;
        $msgService = new MsgServices();

        $now = time();
        $timeSt = $now + 86400;
        $timeEd = $now + 86400 + 60;

        $list = $relationModel->getUserRelationByEndTime($timeSt, $timeEd);
        if(!empty($list)) {
            $userIds = array_merge(array_column($list, 'user_id_1'), array_column($list, 'user_id_2'));
            $userList = $userModel->get_users_by_ids($userIds);
            $userList = array_column($userList, null, 'user_id');

            $rankList = $relationModel->getRelationRank();
            $rankList = array_column($rankList, null, 'rank_id');

            foreach($list as $val) {
                if(empty($userList[$val['user_id_1']]) || empty($userList[$val['user_id_2']]) || empty($rankList[$val['rank_id']])) {
                    continue;
                }
                $userInfo1 = $userList[$val['user_id_1']];
                $userInfo2 = $userList[$val['user_id_2']];
                $rankInfo = $rankList[$val['rank_id']];

                //系统消息
                $msgService->sendUserWithUrlMsg($val['user_id_1'], '您与'.$userInfo2['nickname'].'的'.$rankInfo['rank_name'].'关系即将消失，快去处理一下吧', 'kkaudio://user_first_page?user_id='.$val['user_id_2'], '去处理');
                $msgService->sendUserWithUrlMsg($val['user_id_2'], '您与'.$userInfo1['nickname'].'的'.$rankInfo['rank_name'].'关系即将消失，快去处理一下吧', 'kkaudio://user_first_page?user_id='.$val['user_id_1'], '去处理');
            }
        }

        return true;
    }

}